package com.yanqu.road.logic.activity.foxcard.game.sorcery;

import com.yanqu.road.entity.activity.foxcard.FoxBaseCard;
import com.yanqu.road.entity.activity.foxcard.FoxHandCard;
import com.yanqu.road.entity.activity.foxcard.config.FoxCardBuffConfig;
import com.yanqu.road.logic.activity.foxcard.game.FoxCardGameLogic;
import com.yanqu.road.logic.activity.foxcard.game.FoxCardShowInfo;
import com.yanqu.road.logic.activity.foxcard.game.eFoxCardShowType;
import com.yanqu.road.utils.RandomHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 选游戏牌给buff
 * 数量|buffid|buffid|buffid|0
 * 多个buffid则表示等权重随机一个
 */
public class FoxCardSorceryUse1 extends FoxCardSorceryUseBase {
    @Override
    protected boolean sorceryUse(FoxCardGameLogic gameLogic) {
        int num = sorceryConfig.getParamList().get(0);
        int targetNum = targetIdList.size();
        if (targetNum <= 0 || targetNum > num) {
            return false;
        }

        List<Integer> buffIds = new ArrayList<>();
        for (int i = 1; i < sorceryConfig.getParamList().size(); i++) {
            Integer buffId = sorceryConfig.getParamList().get(i);
            if (buffId > 0) {
                buffIds.add(buffId);
            }
        }
        if (buffIds.isEmpty()) {
            return false;
        }

        Integer buffId = RandomHelper.getRandomList(buffIds, 1).get(0);
        FoxCardBuffConfig buffConfig = gameLogic.getConfig().getBuffConfigMap().get(buffId);
        if (buffConfig == null) {
            return false;
        }


        List<FoxBaseCard> triggerCards = new ArrayList<>();
        int showType = 0;
        for (Integer handCardId : targetIdList) {
            FoxHandCard foxHandCard = gameLogic.getGameData().getHandCards().stream().filter(handCard -> handCard.getId() == handCardId).findFirst().orElse(null);
            if (foxHandCard == null) {
                return false;
            }
            if (buffConfig.getSort() == 1) {
                if (foxHandCard.getReinforceEffect() > 0) {
                    return false;
                }
                foxHandCard.setReinforceEffect(buffId);
                triggerCards.add(foxHandCard);
                showType = eFoxCardShowType.REINFORCE_EFFECT.getValue();
            } else if (buffConfig.getSort() == 2) {
                if (foxHandCard.getVersionEffect() > 0) {
                    return false;
                }
                foxHandCard.setVersionEffect(buffId);
                triggerCards.add(foxHandCard);
                showType = eFoxCardShowType.VERSION_EFFECT.getValue();
            } else if (buffConfig.getSort() == 3) {
                if (foxHandCard.getSealEffect() > 0) {
                    return false;
                }
                foxHandCard.setSealEffect(buffId);
                triggerCards.add(foxHandCard);
                showType = eFoxCardShowType.SEAL_EFFECT.getValue();
            }
        }

        if (triggerCards.isEmpty()) {
            return false;
        }
        FoxCardShowInfo showInfo = gameLogic.newFoxShow(showType);
        showInfo.setTriggerCards(triggerCards);
        showInfo.setShowValue(buffId);

        return true;
    }
}
